package com.wx.task;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestThread {
    private static volatile long count = 0;

    private  void add10K() {
        int idx = 0;
        while (idx++ < 10000) {
            count += 1;
        }
    }

    public  static long calc() throws InterruptedException {
        final TestThread test = new TestThread();
        // 创建两个线程，执行 add() 操作
        Thread th1 = new Thread(() -> {
            test.add10K();
        });
        Thread th2 = new Thread(() -> {
            test.add10K();
        });
        // 启动两个线程
        th1.start();
        th2.start();
        // 等待两个线程执行结束
        th1.join();
        th2.join();
        return count;
    }

    public static void main(String[] args) {
        try {
            long calc = TestThread.calc();
            log.info("两个线程一起加 ：{}",calc);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}